using UnityEngine;

namespace Gj
{
    /// <summary>
    ///     对象基础control 网络层控制绑定
    /// </summary>
    public class BaseLogic : MonoBehaviour, Logic
    {
        public int index;

        private GameObject _gameObject;
        protected bool active;

        public GameObject GameObject
        {
            get
            {
                if (_gameObject == null) _gameObject = gameObject;
                return _gameObject;
            }
        }

        protected virtual void OnDestroy()
        {
            AiLogicService.single.DestroyLogic(this);
        }

        public int GetIndex()
        {
            return index;
        }

        public bool IsActive()
        {
            return active;
        }

        public virtual void Init()
        {
        }

        /// <summary>
        ///     创建对象初始化函数
        /// </summary>
        /// <param name="i"></param>
        /// <param name="e"></param>
        public void Init(int i)
        {
            index = i;
            active = true;

            Init();
        }

        protected virtual void OnRecovery()
        {
        }

        public void Recovery()
        {
            OnRecovery();
            Destroy();
        }

        private void Destroy()
        {
            active = false;
            OnDestroy();
        }
    }
}